import { App } from 'ant-design-vue';
import { MessageInstance } from 'ant-design-vue/es/message';
import { NotificationInstance } from 'ant-design-vue/es/notification/interface';
import { ModalStaticFunctions } from 'ant-design-vue/es/modal/confirm';

let message: MessageInstance;
let notification: NotificationInstance;
let modal: Omit<ModalStaticFunctions, 'warn'>;

let staticFunction: ReturnType<typeof App.useApp> | null = null;

/**
 * 使用AppFeedback Hook
 * @description
 * - 基于App.useApp()，满足config provider 的上下文
 * - notification, modal, message 的统一使用入口
 * - 单例模式，确保全局仅调用一次App.useApp()
 */
export function useAppFeedback() {
  if (!staticFunction) {
    staticFunction = App.useApp();
    // @ts-ignore
    message = staticFunction.message;
    modal = staticFunction.modal;
    notification = staticFunction.notification;
  }

  return { notification, modal, message };
}
